<?php
class UserSaveSetupAction extends FrontendAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('user','setup');
	}
	
	public function execute()
	{
		if($this->data['user']['id']!='' && $this->data['user']['username']=='')
		{
			$data			= $this->getValidData();
			$username		= strip_tags(trim($data['username']));
			$alipayAccount	= strip_tags(trim($data['alipay_account']));
			
			$userService	= C('UserService');
			
			$tmp	= $userService->getUserByName($username);
			if($tmp)
			{
				$this->data['error_messages']['username']	= array('lower'=>'该昵称已经被其他用户使用');
			}
			
			//验证通过，更新数据库
			if(empty($this->data['error_messages']))
			{
				$update		= array('username'=>$username,'alipay_account'=>$alipayAccount);
				//获取头像
				$headImage	= $userService->getTaobaoHeadImage($this->data['user']['taobao_id']);
				if(!empty($headImage))
				{
					$update['logo']	= $headImage;
				}
				//如果设置了注册佣金，更新用户的余额
				$returnSetting	= C('SettingService')->get('return_setting',true);
				if($returnSetting['rate']>0 && $returnSetting['reg_money']>0)
				{
					$update['money']	= $returnSetting['reg_money'];
				}
				//更新信息
				$userService->update($this->data['user']['id'], $update);
				
				$this->code	= 200;
				$this->data['forward'] = C('LtUrl')->generate('User', 'Index');
			}
			else
			{
				$this->code	= 500;
			}
		}
		else
		{
			$this->code	= 500;
		}
	}
}
